package com.bt.lms.dao.servise.impl;


import com.bt.lms.bo.TeachingPlanCondition;
import com.bt.lms.constant.BOConstant;
import com.bt.lms.entity.JdbcUtils;
import com.bt.lms.entity.TeachingPlanBasicInfo;
import com.bt.lms.dao.servise.TeachingPlanDao;
import com.bt.lms.bo.Page;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 实现教学计划Dao接口
 * @author 李舒淇
 */
public class TeachingPlanDaoImpl implements TeachingPlanDao {


    @Override
    public int insert(String sql, TeachingPlanBasicInfo teachingPlanBasicInfo) throws Exception {


        String code = teachingPlanBasicInfo.getCode();
        String name = teachingPlanBasicInfo.getName();
        Integer learningCenter = teachingPlanBasicInfo.getLearningCenter();
        Integer applyLevel = teachingPlanBasicInfo.getApplyLevel();
        Integer lengthOfSchoolingText = teachingPlanBasicInfo.getLengthOfSchoolingText();
        Integer lengthOfSchooling = teachingPlanBasicInfo.getLengthOfSchooling();
        Integer numberOfRepairableTimes = teachingPlanBasicInfo.getNumberOfRepairableTimes();
        Integer status = teachingPlanBasicInfo.getStatus();
        String currentTerm = teachingPlanBasicInfo.getCurrentTerm();

        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);

        pS.setString(1,code);
        pS.setString(2,name);
        pS.setInt(3,learningCenter);
        pS.setInt(4,applyLevel);
        pS.setInt(5,lengthOfSchoolingText);
        pS.setInt(6,lengthOfSchooling);
        pS.setInt(7,numberOfRepairableTimes);
        pS.setInt(8,status);
        pS.setString(9,currentTerm);

        //4.执行sql语句
        int i = pS.executeUpdate();


        //5.关闭资源
        pS.close();
        jdbc.close();




        return i;
    }


    @Override
    public TeachingPlanBasicInfo select(String sql, TeachingPlanBasicInfo teachingPlanBasicInfo,Long id) throws Exception {

        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);
        pS.setLong(1,id);
        ResultSet resultSet = pS.executeQuery();
        boolean next = resultSet.next();
        if (next){
            //获取数据库的内容
            String code = resultSet.getString("code");
            String name = resultSet.getString("name");
            int learningCenter = resultSet.getInt("learning_Center");
            int applyLevel = resultSet.getInt("apply_Level");
            int lengthOfSchoolingText = resultSet.getInt("length_Of_Schooling_text");
            int lengthOfSchooling = resultSet.getInt("length_Of_Schooling");
            int numberOfRepairableTimes = resultSet.getInt("number_of_repairable_times");
            int status = resultSet.getInt("status");
            int relCourseQuantity = resultSet.getInt("rel_course_quantity");
            String currentTerm = resultSet.getString("current_term");

            //带走
            teachingPlanBasicInfo.setCode(code);
            teachingPlanBasicInfo.setName(name);
            teachingPlanBasicInfo.setLearningCenter(learningCenter);
            teachingPlanBasicInfo.setApplyLevel(applyLevel);
            teachingPlanBasicInfo.setLengthOfSchoolingText(lengthOfSchoolingText);
            teachingPlanBasicInfo.setLengthOfSchooling(lengthOfSchooling);
            teachingPlanBasicInfo.setNumberOfRepairableTimes(numberOfRepairableTimes);
            teachingPlanBasicInfo.setStatus(status);
            teachingPlanBasicInfo.setRelCourseQuantity(relCourseQuantity);
            teachingPlanBasicInfo.setCurrentTerm(currentTerm);

        }




        return teachingPlanBasicInfo;
    }

    @Override
    public boolean edit(String sql, TeachingPlanBasicInfo teachingPlanBasicInfo,Long id) throws Exception {


        String code = teachingPlanBasicInfo.getCode();
        String name = teachingPlanBasicInfo.getName();
        Integer learningCenter = teachingPlanBasicInfo.getLearningCenter();
        Integer applyLevel = teachingPlanBasicInfo.getApplyLevel();
        Integer lengthOfSchoolingText = teachingPlanBasicInfo.getLengthOfSchoolingText();
        Integer lengthOfSchooling = teachingPlanBasicInfo.getLengthOfSchooling();
        Integer numberOfRepairableTimes = teachingPlanBasicInfo.getNumberOfRepairableTimes();
        Integer status = teachingPlanBasicInfo.getStatus();
        Integer relCourseQuantity = teachingPlanBasicInfo.getRelCourseQuantity();
        String currentTerm = teachingPlanBasicInfo.getCurrentTerm();

        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);
        pS.setLong(11,id);

        pS.setString(1,code);
        pS.setString(2,name);
        pS.setInt(3,learningCenter);
        pS.setInt(4,applyLevel);
        pS.setInt(5,lengthOfSchoolingText);
        pS.setInt(6,lengthOfSchooling);
        pS.setInt(7,numberOfRepairableTimes);
        pS.setInt(8,status);
        pS.setInt(9,relCourseQuantity);
        pS.setString(10,currentTerm);

        //4.执行sql语句
        int i = pS.executeUpdate();


        //5.关闭资源
        pS.close();
        jdbc.close();



        return false;
    }


    @Override
    public boolean enable(String sql,Long id) throws Exception {

        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);
        pS.setInt(1, BOConstant.TEACHING_PLAN_BASIC_INFO_STATUS_ENABLE);
        pS.setLong(2,id);
        //4.执行sql语句
        int i = pS.executeUpdate();

        //5.关闭资源
        pS.close();
        jdbc.close();


        return true;
    }

    @Override
    public boolean delete(String sql,Long id) throws Exception {

        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);
        pS.setInt(1, BOConstant.DELETED_TRUE);
        pS.setLong(2,id);

        //4.执行sql语句
        int i = pS.executeUpdate();

        //5.关闭资源
        pS.close();
        jdbc.close();


        return false;
    }

    @Override
    public boolean accomplish(String sql, Long id) throws Exception {

        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);
        pS.setInt(1, BOConstant.TEACHING_PLAN_BASIC_INFO_STATUS_DRAFT_COMPLETE);
        pS.setLong(2,id);

        //4.执行sql语句
        int i = pS.executeUpdate();

        //5.关闭资源
        pS.close();
        jdbc.close();

        return false;
    }

    @Override
    public Page<TeachingPlanBasicInfo> query(String sql, Page<TeachingPlanCondition> page, Page<TeachingPlanBasicInfo> teachingPage) throws SQLException {



        List<TeachingPlanBasicInfo> records = new ArrayList<>();


        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);


        //默认等前为第一页
        Integer current = teachingPage.getCurrentPageNo();
        //默认每页条数为10条
        Integer size = teachingPage.getPageSize();
        int startSize = size * (current - 1);
        pS.setInt(1,startSize);
        pS.setInt(2,size);

        //执行sql
        ResultSet resultSet = pS.executeQuery();
        int i = 0;
        while (resultSet.next()){
            TeachingPlanBasicInfo teachingPlanBasicInfo = new TeachingPlanBasicInfo();
            long id = resultSet.getLong("id");
            String code = resultSet.getString("code");
            String name = resultSet.getString("name");
            int learningCenter = resultSet.getInt("learning_Center");
            int applyLevel = resultSet.getInt("apply_Level");
            int lengthOfSchoolingText = resultSet.getInt("length_Of_Schooling_text");
            int lengthOfSchooling = resultSet.getInt("length_Of_Schooling");
            int numberOfRepairableTimes = resultSet.getInt("number_of_repairable_times");
            int status = resultSet.getInt("status");
            int relCourseQuantity = resultSet.getInt("rel_course_quantity");
            String currentTerm = resultSet.getString("current_term");

            teachingPlanBasicInfo.setID(id);
            teachingPlanBasicInfo.setCode(code);
            teachingPlanBasicInfo.setName(name);
            teachingPlanBasicInfo.setLearningCenter(learningCenter);
            teachingPlanBasicInfo.setApplyLevel(applyLevel);
            teachingPlanBasicInfo.setLengthOfSchoolingText(lengthOfSchoolingText);
            teachingPlanBasicInfo.setLengthOfSchooling(lengthOfSchooling);
            teachingPlanBasicInfo.setNumberOfRepairableTimes(numberOfRepairableTimes);
            teachingPlanBasicInfo.setStatus(status);
            teachingPlanBasicInfo.setRelCourseQuantity(relCourseQuantity);
            teachingPlanBasicInfo.setCurrentTerm(currentTerm);

            records.add(i,teachingPlanBasicInfo);
            i++;
        }
        //
        teachingPage.setTableData(records);

        //关闭资源
        pS.close();
        jdbc.close();

        return teachingPage;
    }

}
